package com.psp.bu.persist.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Repository;

import com.psp.bu.model.ProductAttributeBean;
import com.psp.bu.model.ProductBean;
import com.psp.bu.model.ProductEditInfoBean;
import com.psp.bu.model.ProductSkuBean;
import com.psp.bu.model.ProductUpdateLogBean;
import com.psp.bu.model.UserProductRelationBean;
import com.psp.bu.persist.dao.ProductDao;

@Repository
public class ProductImpl extends BaseImpl implements ProductDao {
	protected static final String NAME_SPACE = NAME_SPACE_HEADER + ".ProductMapper";

	@Override
	public ProductBean selectById(String pid) {
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectById", pid);
	}

	@Override
	public List<ProductBean> selectByBuidAndList(String buid, List<String> pids) {
		Map<String, Object> where = new HashMap<String, Object>();
		where.put("pids", pids);
		where.put("buid", buid);
		return sqlSessionTemplate.selectList(NAME_SPACE + ".selectByBuidAndList", where);
	}

	@Override
	public int updateStateBatch(List<String> pids, int state) {
		Map<String, Object> where = new HashMap<String, Object>();
		where.put("pids", pids);
		where.put("state", state);
		if (state == 1) {
			where.put("onShelfDate", new Date());
		} else if (state == 2) {
			where.put("offShelfDate", new Date());
		}

		return sqlSessionTemplate.update(NAME_SPACE + ".updateStateBatch", where);
	}

	@Override
	public int updateIsDeleteBatch(List<String> pids, Integer isDelete) {
		Map<String, Object> where = new HashMap<String, Object>();
		where.put("pids", pids);
		where.put("isDelete", isDelete);
		return sqlSessionTemplate.delete(NAME_SPACE + ".updateIsDeleteBatch", where);
	}

	@Override
	public int deleteUserProductRelationBatch(String buid, List<String> pids) {
		Map<String, Object> where = new HashMap<String, Object>();
		where.put("pids", pids);
		where.put("buid", buid);
		return sqlSessionTemplate.delete(NAME_SPACE + ".deleteUserProductRelationBatch", where);
	}

	@Override
	public List<ProductBean> selectByPids(Set<String> pids) {
		Map<String, Object> where = new HashMap<>();
		where.put("pids", pids);
		return sqlSessionTemplate.selectList(NAME_SPACE + ".selectByPids", where);
	}

	@Override
	public ProductSkuBean selectProductInfoBySukCode(String skuCode, Integer state) {
		Map<String, Object> where = new HashMap<>();
		where.put("skuCode", skuCode);
		where.put("state", state);
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectProductInfoBySukCode", where);
	}

	@Override
	public List<ProductAttributeBean> selectAttributeList(String pid) {
		return sqlSessionTemplate.selectList(NAME_SPACE + ".selectAttributeList", pid);
	}

	@Override
	public int selectProductCount(String buid, int shelfState) {
		Map<String, Object> where = new HashMap<>();
		where.put("buid", buid);
		where.put("state", shelfState);
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectProductCount", where);

	}

	@Override
	public List<ProductBean> selectProductList(int page, int pageSize, String buid, int shelfState) {
		Map<String, Object> where = new HashMap<>();
		where.put("start", page * pageSize);
		where.put("length", pageSize);
		where.put("buid", buid);
		where.put("state", shelfState);
		return sqlSessionTemplate.selectList(NAME_SPACE + ".selectProductList", where);

	}

	@Override
	public int insert(ProductBean product) {
		return sqlSessionTemplate.insert(NAME_SPACE + ".insert", product);
	}

	@Override
	public int insertUserProduct(UserProductRelationBean userproduct) {
		return sqlSessionTemplate.insert(NAME_SPACE + ".insertUserProduct", userproduct);
	}

	@Override
	public int inserProductEditInfo(ProductEditInfoBean eidtInfo) {
		return sqlSessionTemplate.insert(NAME_SPACE + ".inserProductEditInfo", eidtInfo);
	}

	@Override
	public ProductEditInfoBean selectEditInfoByCode(Integer pcode) {
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectEditInfoByCode", pcode);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public int update(ProductBean product) {
		return sqlSessionTemplate.update(NAME_SPACE + ".update", product);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public int updateProductEditInfo(ProductEditInfoBean eidtInfo) {
		return sqlSessionTemplate.update(NAME_SPACE + ".updateProductEditInfo", eidtInfo);
	}

	@Override
	public int insertUpdateLog(ProductUpdateLogBean update) {
		return sqlSessionTemplate.insert(NAME_SPACE + ".insertUpdateLog", update);
	}
}
